package com.miracle.common.util;

import android.content.Context;
import android.content.SharedPreferences;

import static com.miracle.common.bean.User.User.*;

/**
 * PreferencesUtils.java
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/6/21 12:51
 * @see com.miracle.common.util
 */
public class PreferencesUtils {
    public static final String PREFERENCES_NAME = "common";

    public static class State {
        /**
         * 用户名
         */
        public static final String USER_NAME = "user_name";
        /**
         * 用户角色
         * {@link ROLE_COMMON,ROLE_VIP,ROLE_ADMIN}
         */
        public static final String USER_ROLE = "user_role";

        /**
         * 用户session
         */
        public static final String USER_SESSIONID = "session_id";
        /**
         * 电话
         */
        public static final String USER_TEL = "user_tel";
        /**
         * 邮箱
         */
        public static final String USER_MAIL = "user_mail";

        /**
         * 性别
         */
        public static final String USER_SEX = "user_sex";

        /**
         * 头像
         */
        public static final String USER_PATH = "user_path";

    }

    public static class Version {
        /**
         * 最新版本号
         */
        public static final String VERSION_NEWEST = "version_newest";
    }

    public static String getPreferences(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context
                .MODE_PRIVATE);
        return preferences.getString(name, "");
    }

    public static void setPreferences(Context context, String name, String value) {
        if (context != null && value != null) {
            SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                    Context.MODE_PRIVATE).edit();
            sharedata.putString(name, value);
            sharedata.commit();
        }
    }

    public static Float getFloatPreferences(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context
                .MODE_PRIVATE);
        return preferences.getFloat(name, 0);
    }

    public static void setFloatPreferences(Context context, String name, float value) {
        if (context != null && value != 0) {
            SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                    Context.MODE_PRIVATE).edit();
            sharedata.putFloat(name, value);
            sharedata.commit();
        }
    }


    public static int getIntPreferences(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context
                .MODE_PRIVATE);
        return preferences.getInt(name, 0);
    }

    public static void setIntPreferences(Context context, String name, int value) {
        SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                Context.MODE_PRIVATE).edit();
        sharedata.putInt(name, value);
        sharedata.commit();
    }

    public static boolean getBooleanPreferences(Context context, String name) {
        if (context == null) {
            return false;
        }
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context
                .MODE_PRIVATE);
        return preferences.getBoolean(name, false);
    }

    public static void setBooleanPreferences(Context context, String name, boolean value) {
        if (context != null) {
            SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                    Context.MODE_PRIVATE).edit();
            sharedata.putBoolean(name, value);
            sharedata.commit();
        }

    }

    public static long getLongPreferences(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context
                .MODE_PRIVATE);
        return preferences.getLong(name, 0L);
    }

    public static void setLongPreferences(Context context, String name, long value) {
        SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                Context.MODE_PRIVATE).edit();
        sharedata.putLong(name, value);
        sharedata.commit();
    }

    /**
     *
     * @param context
     */
    public static void reset(Context context) {
        SharedPreferences.Editor sharedata = context.getSharedPreferences(PREFERENCES_NAME,
                Context.MODE_PRIVATE).edit();
        sharedata.clear().commit();
    }
}
